package service

import (
	"ez/apps/tool/document"
	"fmt"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	tools "gitee.com/dreamwood/ez-go/tools"
	"regexp"
	"strconv"
	"strings"
)

func CreateSnByString(service string, code string, saveValue int) string {
	service = ez.ConfigService.AppId //覆盖
	rep := tool.NewComSnCrud()
	sn, e := rep.FindOneBy(ss.M{"text": code, "service": service}, nil)
	ez.PrintError(e)
	if sn.Id == 0 {
		sn.Text = code
		sn.Name = code
		sn.Service = service
		sn.Save()
	}
	return CreateSn(sn, saveValue)
}

func CreateSn(sn *tool.ComSn, saveValue int) string {
	sntext := sn.Text

	//日期标记的处理
	nowString := tools.GetDateYMDHIS("", "", "")
	dateMap := make(map[string]string)
	//202106071533
	//println(nowString)
	dateMap["[YY]"] = nowString[0:4]
	dateMap["[MM]"] = nowString[4:6]
	dateMap["[DD]"] = nowString[6:8]
	dateMap["[HH]"] = nowString[8:10]
	dateMap["[II]"] = nowString[10:12]
	dateMap["[SS]"] = nowString[12:14]
	for find, replace := range dateMap {
		sntext = strings.ReplaceAll(sntext, find, replace)
	}
	fmt.Printf("%s", dateMap)
	//随机数处理[RND[X]] X为长度数
	tmp := make([]int, 15)
	for i, _ := range tmp {
		if i < 2 {
			continue
		}
		//没有做正则匹配默认最多允许20位的长度，CreateRand 处理int数据早就报错了
		//println(i)
		find := fmt.Sprintf("[RND[%s]]", strconv.Itoa(i+1))
		//println(find)
		//println(tool.CreateRand(i+5))
		sntext = strings.ReplaceAll(sntext, find, tools.CreateRand(i+1))
		//i++

	}
	//println("here")
	//处理累加最大 MAX[3],DMAX,WMAX,MMAX,YMAX
	pattern := `\[([A-Z])?MAX(\[(\d)\])?\]`
	reg := regexp.MustCompile(pattern)
	findsMax := reg.FindAllStringSubmatch(sntext, -1)
	//fmt.Printf("%s",findsMax)
	for _, matchSlice := range findsMax {
		prefix := matchSlice[1]
		maxNum := matchSlice[3]

		where := ss.M{}
		where["snId"] = sn.Id
		typeValue := ""
		if prefix == "" { //累加最大
			//where["type"] = "all"
			//typeValue = "all"
			//prefix = "all"
			continue
		} else {
			where["type"] = prefix
			if prefix == "Y" {
				typeValue = nowString[0:4]
			}
			if prefix == "M" {
				typeValue = nowString[0:6]
			}
			if prefix == "D" {
				typeValue = nowString[0:8]
			} else {
				typeValue = nowString[0:4]
			}
		}
		where["typeValue"] = typeValue
		snLogRep := tool.NewComSnLogCrud()
		snLog, e := snLogRep.FindOneBy(where, nil)
		ez.PrintError(e)
		if snLog.Id == 0 {
			snLog.SnId = sn.Id
			snLog.Value = typeValue
			snLog.Type = prefix
			value := fmt.Sprintf("%0"+maxNum+"d", 1)
			snLog.Value = value
			snLog.TypeValue = typeValue
			snLog.Save()
		}
		findMax := matchSlice[0]
		sntext = strings.ReplaceAll(sntext, findMax, snLog.Value)
		if saveValue == 1 {
			nextV, _ := strconv.Atoi(snLog.Value)
			snLog.Value = fmt.Sprintf("%0"+maxNum+"d", nextV+1)
			snLog.Save()
		}
	}
	for _, matchSlice := range findsMax {
		prefix := matchSlice[1]
		maxNum := matchSlice[3]

		where := ss.M{}
		where["snId"] = sn.Id
		typeValue := ""
		if prefix == "" { //累加最大
			where["type"] = "all"
			typeValue = "all"
			prefix = "all"
		} else {
			continue
		}
		where["typeValue"] = typeValue
		snLogRep := tool.NewComSnLogCrud()
		snLog, e := snLogRep.FindOneBy(where, nil)
		ez.PrintError(e)
		if snLog.Id == 0 {
			snLog.SnId = sn.Id
			snLog.Value = typeValue
			snLog.Type = prefix
			value := fmt.Sprintf("%0"+maxNum+"d", 1)
			snLog.Value = value
			snLog.TypeValue = typeValue
			snLog.Save()
		}
		findMax := matchSlice[0]
		sntext = strings.ReplaceAll(sntext, findMax, snLog.Value)
		if saveValue == 1 {
			nextV, _ := strconv.Atoi(snLog.Value)
			//snLog.Value = strconv.Itoa(nextV+1)
			snLog.Value = fmt.Sprintf("%0"+maxNum+"d", nextV+1)
			snLog.Save()
		}
	}

	//自定义字段替换

	//patternDiy := `\[(A-Z)+\]`
	//regDiy:=regexp.MustCompile(patternDiy)
	//findsDiyMax := regDiy.FindAllStringSubmatch(sntext,-1)
	//todo 去他妈的，先不做了
	//print(findsDiyMax)
	return sntext
}
